﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Pap.Data
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public partial class Committee : ICommittee
    {
        #region ICommittee Members


        /// <summary>
        /// Gets or sets the villages.
        /// </summary>
        /// <value>
        /// The villages.
        /// </value>
        public IEnumerable<IVillage> Villages
        {
            get
            {
                return this.CommittessVillages.Select(v => v.Village).OfType<IVillage>();
            }
            set
            {
                foreach (Village villageitem in value)
                {
                    this.CommittessVillages.Add(new CommittessVillage { VillageID = villageitem.VillageID });
                }
            }
        }



        /// <summary>
        /// Changes the villages.
        /// </summary>
        /// <param name="entities">The entities.</param>
        public void ChangeVillages(IEnumerable<IVillage> entities)
        {
            var deleted = this.CommittessVillages.Where(t => !entities.Select(v => v.VillageID).Contains(t.VillageID)).ToList();
            var inserted = entities.Where(t => !this.CommittessVillages.Select(v => v.VillageID).Contains(t.VillageID));

            foreach (var item in deleted)
            {
                this.CommittessVillages.Remove(item);
            }

            var sd = deleted.ToList();
            foreach (var item in inserted)
            {
                this.CommittessVillages.Add(new CommittessVillage { VillageID = item.VillageID, CommitteID = this.CommitteeID });
            }

        }

        #endregion

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return CommitteeName;
        }


        /// <summary>
        /// Gets the committee bank accounts.
        /// </summary>
        IEnumerable<ICommitteeBankAccount> ICommittee.CommitteeBankAccounts
        {
            get { return this.CommitteeBankAccounts.OfType<ICommitteeBankAccount>(); }
        }

    }
}
